package com.deyuanyun.pic.settings.util;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Element;
import com.itextpdf.text.log.Level;
import com.itextpdf.text.log.Logger;
import com.itextpdf.text.log.LoggerFactory;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.xml.XMLUtil;
import com.itextpdf.tool.xml.NoCustomContextException;
import com.itextpdf.tool.xml.Tag;
import com.itextpdf.tool.xml.WorkerContext;
import com.itextpdf.tool.xml.css.CssUtils;
import com.itextpdf.tool.xml.exceptions.LocaleMessages;
import com.itextpdf.tool.xml.exceptions.RuntimeWorkerException;
import com.itextpdf.tool.xml.html.AbstractTagProcessor;
import com.itextpdf.tool.xml.html.HTML;
import com.itextpdf.tool.xml.net.ImageRetrieve;
import com.itextpdf.tool.xml.net.exc.NoImageException;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 模块描述
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/6/23）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
public class Image extends AbstractTagProcessor {

    private final CssUtils utils = CssUtils.getInstance();
    private static final Logger logger = LoggerFactory.getLogger(Image.class);


    /*
     * (non-Javadoc)
     *
     * @see
     * com.itextpdf.tool.xml.TagProcessor#endElement(com.itextpdf.tool.xml.Tag,
     * java.util.List, com.itextpdf.text.Document)
     */
    @Override
    public List<Element> end(final WorkerContext ctx, final Tag tag, final List<Element> currentContent) {
        Map<String, String> attributes = tag.getAttributes();
        String src = attributes.get(HTML.Attribute.SRC);
        com.itextpdf.text.Image img = null;
        List<Element> l = new ArrayList<Element>(1);
        if (null != src && src.length() > 0) {
            // check if the image was already added once
            src = XMLUtil.unescapeXML(src);
            src = src.trim();
            try {
                if (logger.isLogging(Level.TRACE)) {
                    logger.trace(String.format(LocaleMessages.getInstance().getMessage(LocaleMessages.HTML_IMG_USE), src));
                }
                if(src.matches("^data:image/.{1,10};base64,.+$")){//base64的图片数据


                    byte[] imgData= Base64.decodeBase64(src.substring(src.indexOf("base64,")+7));
                    try {
                        img=com.itextpdf.text.Image.getInstance(imgData);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }else{
                    HtmlPipelineContext context = getHtmlPipelineContext(ctx);
                    img = new ImageRetrieve(context.getResourcesRootPath(), context.getImageProvider()).retrieveImage(src);
                }
            } catch (NoImageException e) {
                if (logger.isLogging(Level.ERROR)) {
                    logger.error(String.format(LocaleMessages.getInstance().getMessage(LocaleMessages.HTML_IMG_RETRIEVE_FAIL), src), e);
                }
            } catch (NoCustomContextException e) {
                throw new RuntimeWorkerException(LocaleMessages.getInstance().getMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e);
            }
            if (null != img) {
                try {
                    if ( attributes.get(HTML.Attribute.ALT) != null) {
                        img.setAccessibleAttribute(PdfName.ALT, new PdfString(attributes.get(HTML.Attribute.ALT)));
                    }
                    HtmlPipelineContext htmlPipelineContext = getHtmlPipelineContext(ctx);
                    l.add(getCssAppliers().apply(new Chunk((com.itextpdf.text.Image) getCssAppliers().apply(img, tag, htmlPipelineContext), 0, 0, true), tag, htmlPipelineContext));
                } catch (NoCustomContextException e) {
                    throw new RuntimeWorkerException(e);
                }
            }
        }
        return l;
    }


    /*
     * (non-Javadoc)
     *
     * @see com.itextpdf.tool.xml.TagProcessor#isStackOwner()
     */
    @Override
    public boolean isStackOwner() {
        return false;
    }
}
